package com.tongcao.cn.algorithm.designMoel.proxy.jdk.cglib;

import org.mockito.cglib.proxy.Enhancer;
import org.mockito.cglib.proxy.MethodInterceptor;
import org.mockito.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author:tongcao
 * @since:2019/6/18 19:15
 * Description: * CGLibProxy动态代理类
 */
public class CGLibProxy implements MethodInterceptor {

    /**
     * CGLib需要代理的目标对象
     */
    private Object targetObj;


    public Object createProxyObject(Object obj) {
        this.targetObj = obj;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(obj.getClass());
        enhancer.setCallback(this);
        Object proxyObj = enhancer.create();
        return proxyObj;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        Object obj = null;
        System.out.println("method:" + method);
        final Object ret = method.invoke(targetObj, objects);
        return ret;
    }
}